package thread.homework.problem2;

import thread.homework.problem1.Flag;

public class B extends Thread {
    private int num = 0;
    private Flag flag;

    public B(Flag flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        while (true) {
            if (num == 10)
                return;
            synchronized (flag) {
                while (flag.getFlagNum() != 1) {
                    try {
                        flag.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("B");
                num++;
                flag.changeFlagNum();
                flag.notifyAll();
            }
        }
    }
}
